import requests
import json


def open_url(url):
    print("信息：正在请求：" + url)
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
    }
    res = requests.get(url, headers=headers)
    return res


def outdata(res, fileName=""):
    if (fileName == ""):
        fileName = "API\\" + res.url.split("/")[len(res.url.split("/")) - 1] + ".json "
    if ("?" in fileName):
        fileName = fileName.split("?")[0] + ".json"
    if ("Not Found" in res.text or "500 Internal Server Error" in res.text):
        print("警告：" + res.url + " 请求失败")
        return False
    with open(fileName, "w") as f:
        f.write(res.text)
        return True


def main():
    baseURL = "http://152.136.185.210:8000/api/h3"
    
    outdata(open_url(baseURL + "/home/multidata"))
    outdata(open_url(baseURL + "/category"))
    outdata(open_url(baseURL + "/recommend"))
    outdata(open_url(baseURL + "/home/subcategory"))

    for n in range(50):
        outdata(open_url(baseURL + "/home/data?" + "type=pop" +"&page=" + str(n+1)), "API\\pop\\pop-" + str(n+1) + ".json")
        outdata(open_url(baseURL + "/home/data?" + "type=new" +"&page=" + str(n+1)), "API\\new\\new-" + str(n+1) + ".json")
        outdata(open_url("http://123.207.32.32:8000/api/h3" + "/home/data?" +"type=sell" + "&page=" + str(n+1)), "API\\sell\\sell-" + str(n+1) + ".json") 
    
    for n in range(50):
        with open("API\\new\\new-" + str(n+1) + ".json", "r", encoding='utf8') as f:
            newJSON = json.loads(f.read())
            for m in range(len(newJSON['data']['list'])):
                iid = newJSON['data']['list'][m]['iid']
                outdata(open_url(baseURL + "/detail?iid=" + iid),"API\\detail\\iid-" + iid + ".json")
        with open("API\\pop\\pop-" + str(n + 1) + ".json", "r", encoding='utf8') as f:
            newJSON = json.loads(f.read())
            for m in range(len(newJSON['data']['list'])):
                iid = newJSON['data']['list'][m]['iid']
                outdata(open_url(baseURL + "/detail?iid=" + iid),"API\\detail\\iid-" + iid + ".json")
        if (n <= 19):
            with open("API\\sell\\sell-" + str(n + 1) + ".json", "r", encoding='utf8') as f:
                newJSON = json.loads(f.read())
                for m in range(len(newJSON['data']['list'])):
                    iid = newJSON['data']['list'][m]['iid']
                    outdata(open_url(baseURL + "/detail?iid=" + iid),"API\\detail\\iid-" + iid + ".json")


if __name__ == '__main__':
    main()
